package cn.jzx.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class ReadDemo {
    public static void main(String[] args) {
        //hadoop 的配置对象 configuration
        Configuration conf = new Configuration();
        //创建分布式文件系统 FileSystem
        try (FileSystem fileSystem = FileSystem.get(conf)) {
            //定义hdfs上的资源路径
            Path path = new Path("/user/root/demo/info.txt");
            //创建一个分布式数据读入点
            try (FSDataInputStream in = fileSystem.open(path)) {
                //将分布式字节流转化为缓冲字符流
                BufferedReader reader=new BufferedReader(new InputStreamReader(in));
                //利用 jdk8 stream 将数据流遍历打印到控制台
                reader.lines().forEach(line-> System.out.println(line));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
